from rest_framework import serializers
from website.models import Link,Category


class ListSerialer(serializers.ModelSerializer):
    class Meta:
        model = Link
        fields = (
            'category',
            'category_name',
            'name',
            'slink'
        )

    category_name = serializers.CharField(max_length=32,read_only=True,source='category.name')



class CategorySerialer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ("name",)


class LinkSerializer(serializers.ModelSerializer):
    class Meta:
        model = Link
        fields = ('__all__')


class CategoryLinkSerializer(serializers.ModelSerializer):
    slink = serializers.SerializerMethodField()
    class Meta:
        model = Category
        fields = ("__all__")



    def get_slink(self, cate):
        link_info = list()
        link_list = cate.link_set.all()
        for l in link_list:
            info = {"name":l.name,"slink":l.slink}
            link_info.append(info)

        return link_info
